import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { createAdmin, getAdmin, adminStatusChange, deleteAdmin } from './admin.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  const data = await getAdmin();
  response.send(data);
}

/**
 * 创建用户
 */
export const store = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取用户
  const { name, password } = request.body;

  // 创建
  try {
    const data = await createAdmin({ name, password });
    response.status(200).send(data);
  } catch (error) {
    next(error);
  }
}

/**
 * 状态
 */
export const status = async (
  request: Request,
  response: Response,
  next: NextFunction,
) => {
  // 获取内容 ID
  const postId = request.body.id;


  // 获取内容
  const post = _.pick(request.body, ['status']);

  // 执行
  try {
    const data = await adminStatusChange(parseInt(postId, 10), post);
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 删除内容
 */
export const destroy = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取删除内容 ID
  const { postId } = request.params;

  // 删除
  try {
    const data = await deleteAdmin(parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}
